package org.ielse.frame;

import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.text.TextUtils;
import android.util.Log;

import org.ielse.frame.util.BaseUtil;

import java.util.Set;

public class BaseService extends Service {

	private final IBinder mBinder = new LocalBinder();

	public class LocalBinder extends Binder {
		public BaseService getService() {
			return BaseService.this;
		}
	}

	@Override public IBinder onBind(Intent intent) {
		return mBinder;
	}

	protected SharedPreferences spfs;

	@Override public void onCreate() {
		super.onCreate();
		logd("onCreate()");
		spfs = App.i().getSharedPreferences();
	}

	@Override public int onStartCommand(Intent intent, int flags, int startId) {
		logd("onStartCommand()");
		return super.onStartCommand(intent, flags, startId);
	}

	@Override public void onDestroy() {
		logd("onDestroy()");
		super.onDestroy();
	}

	public void logi(String text) {
		if (C.settings.DEBUG && !TextUtils.isEmpty(text)) Log.i(getClass().getSimpleName(), text);
	}

	public void logd(String text) {
		if (C.settings.DEBUG && !TextUtils.isEmpty(text)) Log.d(getClass().getSimpleName(), text);
	}

	public void logw(String text) {
		if (C.settings.DEBUG && !TextUtils.isEmpty(text)) Log.w(getClass().getSimpleName(), text);
	}

	public void loge(String text) {
		if (C.settings.DEBUG && !TextUtils.isEmpty(text)) Log.e(getClass().getSimpleName(), text);
	}

	public void writeToSpfs(String key, Object value) {
		Editor editor = spfs.edit();
		if (value instanceof String) {
			editor.putString(key, (String) value);
		} else if (value instanceof Integer) {
			editor.putInt(key, (Integer) value);
		} else if (value instanceof Boolean) {
			editor.putBoolean(key, (Boolean) value);
		} else if (value instanceof Float) {
			editor.putFloat(key, (Float) value);
		} else if (value instanceof Long) {
			editor.putLong(key, (Long) value);
		} else {
			throw new IllegalArgumentException("not support type");
		}
		editor.commit();
	}

	public String readStringFromSpfs(String key) {
		return readStringFromSpfs(key, null);
	}

	public String readStringFromSpfs(String key, String defValue) {
		return spfs.getString(key, defValue);
	}

	public int readIntFromSpfs(String key) {
		return readIntFromSpfs(key, 0);
	}

	public int readIntFromSpfs(String key, int defValue) {
		return spfs.getInt(key, defValue);
	}

	public float readFloatFromSpfs(String key) {
		return readFloatFromSpfs(key, 0f);
	}

	public float readFloatFromSpfs(String key, float defValue) {
		return spfs.getFloat(key, defValue);
	}

	public boolean readBooleanFromSpfs(String key) {
		return readBooleanFromSpfs(key, false);
	}

	public boolean readBooleanFromSpfs(String key, boolean defValue) {
		return spfs.getBoolean(key, defValue);
	}

	public long readLongFromSpfs(String key) {
		return readLongFromSpfs(key, 0l);
	}

	public long readLongFromSpfs(String key, long defValue) {
		return spfs.getLong(key, defValue);
	}

	public Set<String> readStringSetFromSpfs(String key) {
		return readStringSetFromSpfs(key, null);
	}

	public Set<String> readStringSetFromSpfs(String key, Set<String> defValues) {
		return spfs.getStringSet(key, defValues);
	}

	public boolean containsFromSpfs(String key) {
		return spfs.contains(key);
	}

	public boolean removeSpfs(String key) {
		return spfs.edit().remove(key).commit();
	}

	public boolean clearSpfs() {
		return spfs.edit().clear().commit();
	}

	public void startActivity(Class<?> targetActivity, Object... params) {
		startActivity(targetActivity, BaseUtil.initBundle(params));
	}

	public void startActivity(Class<?> targetActivity, Bundle bundle) {
		Intent intent = new Intent(this, targetActivity);
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intent.putExtras(bundle);
		startActivity(intent);
	}
}
